<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img class="homeImg" src="../assets/image/home/电商后台.jpg" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button @click="loginOut" type="danger">退出登录</el-button>
    </el-header>
    <el-container>
      <el-aside :width="flodFlag ? '64px':'200px'">
        <!-- 侧边栏 -->
        <div class="foldMenuButton" @click="foldMenu">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse="flodFlag"
          :collapse-transition="false"
          :router="true"    
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.path + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              v-for="items in item.children"
              :key="items.id"
              :index="'/' + items.path"
              @click="saveNavState('/' + items.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ items.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体区 -->
      <el-main>
          <!-- 路由占位符号 -->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      flodFlag: false,
      activePath: '' //被激活的链接地址
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginOut() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //获取菜单
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data;
      // console.log(this.menuList)
      // console.log(res)
    },
    //折叠按钮
    foldMenu() {
      this.flodFlag = !this.flodFlag;
    },
    //保存链接的激活状态
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath;
    },
  },
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.homeImg {
  width: 60px;
  height: 53px;
  border-radius: 50%;
  -webkit-border-radius: 50%;
  -moz-border-radius: 50%;
}
.el-header {
  background-color: #373b41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0px;
  color: #fff;
  font-size: 30px;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  > .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaeaf1;
}
.iconfont {
  margin-right: 10px;
}
.foldMenuButton {
  background-color: #4a5064;
  width: 100%;
  text-align: center;
  line-height: 24px;
  font-size: 10px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
